
const Define = require('./ExportDefine')
var fs = require('fs');
var path = require('path');
var file_util = require('../public/file_util');
var coder = require('../public/coder')
function ExportLang() {

}

ExportLang.prototype.getLangObj = function (excelList) {
    let langObj = {}
    for (let index = 0; index < excelList.length; index++) {
        const element = excelList[index];
        let obj = this.getSheetLangObj(element.sheetList);
        for (const key in obj) {
            if (Object.hasOwnProperty.call(obj, key)) {
                const value = obj[key];
                langObj[key] = value;
            }
        }
    }
    return langObj;
}
ExportLang.prototype.getSheetLangObj = function (sheetList) {
    let langObj = {}
    for (let index = 0; index < sheetList.length; index++) {
        const element = sheetList[index];
        // console.log(' element.declareObj  ', element.declareObj)
        for (const key in element.langObj) {
            if (Object.hasOwnProperty.call(element.langObj, key)) {
                const value = element.langObj[key];
                langObj[key] = value
            }
        }
    }
    return langObj;
}
ExportLang.prototype.export = function (export_dir, excelList) {
    // var res_path = path.join(export_dir, 'lang')
    // file_util.mkdir(res_path);
    var langObj = {};
    langObj["data"] = this.getLangObj(excelList);
    langObj["type"] = 2;
    langObj['name'] = 'GameLangModel'
    // console.log('langData ', langData)
    let datas = langObj["data"];
    let textMap = {}
    let exports = '\uFEFF'
    for (let i = 0; i < Define.TEXT.length; i++) {
        textMap[Define.TEXT[i]] = Define.TEXT[i];
    }
    for (var key in datas) {
        // console.log(' key ',key,' value ',jsonObj[key]);
        let ss = datas[key];
        for (let i = 0; i < ss.length; i++) {
            textMap[ss[i]] = ss[i];
        }

    }
    for (var key in textMap) {
        exports += textMap[key]
    }

    let export_text_path = path.join(export_dir, Define.TEXT_PATH)
    file_util.createDir(export_text_path)
    file_util.writeFile(path.join(export_text_path, Define.moduleName + '_font.txt'), exports);
    let export_lang_path = path.join(export_dir, Define.LANG_PATH)
    file_util.createDir(export_lang_path)
    file_util.writeFile(path.join(export_lang_path, Define.moduleName + '_lang_zh.json'), JSON.stringify(coder.pack(langObj, 0),null,2));
}

module.exports = ExportLang;